package cn.com.yinhex.util;

/**
 * 
 * @author cc_admin
 * 
 */

public class IdUtil {
    static final int BIT = 32; // 32进制

    /**
     * 
     */
    public IdUtil() {
    }

    /**
     * 十进制转换成32进制 Author MR Tan
     * 
     * @param i
     *            long
     * @param shift
     *            int
     * @return String
     */
    public static String toUnsigned32(long i) {
        int shift = 5;
        char[] buf = new char[BIT];
        int charPos = BIT;
        int radix = 1 << shift;
        long mask = radix - 1;
        do {
            buf[--charPos] = digits32[(int) (i & mask)];
            i >>>= shift;
        } while (i != 0);
        return new String(buf, charPos, (BIT - charPos));
    }

    /**
     * 进制数
     */
    final static char[] digits32 = { 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'R', 'S', 'T', 'U',
            'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f' };

    /**
     * 将32进制转换成10进制
     * 
     * @param src
     *            String
     * @return long
     */
    public static long toDecimal32(String src) {
        long result = 0;
        int len = src.length();
        int num = len;
        int b = 0;
        String single = null;
        for (int i = 0; i < len; i++) {
            single = src.substring(i, i + 1);
            single = String.valueOf(getNum32(single));
            b = Integer.parseInt(single);
            result = result + b * (long) Math.pow(32, (num - 1));
            num--;
        }
        return result;
    }

    public static int getNum32(String single) {
        int result = 0;
        for (int n = 0; n < 32; n++) {
            String str = String.valueOf(digits32[n]);
            if (single.equals(str)) {
                result = n;
                break;
            }
        }
        return result;
    }

    /**
     * @param lastid
     * @param length
     * @return
     */
    public static String formatId(String lastid, int length) {
        if (lastid.length() < length) {
            StringBuffer buf = new StringBuffer();
            for (int i = 0; i < length; i++) {
                buf.append("0");
            }
            return buf.toString().substring(0, length - lastid.length())
                    + lastid;
        }
        return lastid;
    }

    /**
     * @param id
     * @param length
     * @return
     */
    public static String formatId(long id, int length) {
        String cid = toUnsigned32(id);
        return formatId(cid, length);
    }

    /**
     * 获得8位ID
     * 
     * @param id
     * @return
     */
    public static String getId(long id) {
        return formatId(id, 8);
    }

    /**
     * 获得4位分类ID
     * 
     * @param pid
     * @param id
     * @return
     */
    public static String getCId(String pid, long id) {
        return pid + formatId(id, 4);
    }
}
